package com.example.day8circleprogress;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.BounceInterpolator;

public class CircleProgress extends View {
    private float mValueSize;
    private int mDefaultSize=500;  //默认大小
    private PointF  mCirclePoint=new PointF();  //中心点
    private Paint mValuePaint;
    private Paint mUnitPaint;   //单位
    private Paint mDescriblePaint;   //描述
    private float mValuePaintSet;  //画笔y轴偏移量
    private Paint mArcPaint;
    private RectF mArcRect;   //矩形区域
    private float mSweepAngle;  //开始扫过的角度
    private float mDefaultAngle;   //默认的弧度
    private float mArcStrokeWidth;   //弧画笔的宽度
    private float mStartAngle;  //开始的弧度
    private Paint mArcProgressPaint;  //进度画笔

    public CircleProgress(Context context) {
        super(context);
        initData();
        initPaint();
        startAnim();
    }

    public CircleProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();
        initPaint();
        startAnim();
    }
    public void initData(){
        mValueSize=80;
        mDefaultSize=500;
        mDefaultAngle=270;
        mArcStrokeWidth=16;
        mStartAngle=135;
        mArcRect=new RectF();
    }

    public void initPaint(){
        mValuePaint=new Paint(Paint.ANTI_ALIAS_FLAG);   //抗锯齿
        mValuePaint.setColor(Color.parseColor("#333333"));
        mValuePaint.setTextSize(50);
        mValuePaint.setTextAlign(Paint.Align.CENTER);

        mArcPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setStrokeWidth(mArcStrokeWidth);   //画笔宽度
        mArcPaint.setStyle(Paint.Style.STROKE);  //空心
        mArcPaint.setStrokeCap(Paint.Cap.ROUND); //画笔帽为圆形
        mArcPaint.setColor(Color.parseColor("#777777"));

        mArcProgressPaint =new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcProgressPaint.setStrokeWidth(mArcStrokeWidth);   //画笔宽度
        mArcProgressPaint.setStyle(Paint.Style.STROKE);  //空心
        mArcProgressPaint.setStrokeCap(Paint.Cap.ROUND); //画笔帽为圆形
        mArcProgressPaint.setColor(Color.BLUE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mDefaultSize,mDefaultSize);

    }

    //控件位置发生变化时
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //计算中心点
        mCirclePoint.x=w>>1;
        mCirclePoint.y=h>>1;
        //计算扇形绘制区域
        mArcRect.top=mArcStrokeWidth/2;
        mArcRect.bottom=h-mArcStrokeWidth/2;
        mArcRect.left=mArcStrokeWidth/2;
        mArcRect.right=w-mArcStrokeWidth/2;


        //计算文字y点的偏移量
        mValuePaintSet=mCirclePoint.y+measureTextHeight(mValuePaint)/2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawText(canvas);
        drawArc(canvas);
    }
    //绘制扇形表
    private void drawArc(Canvas canvas){
        //保存画布
        canvas.save();
        canvas.rotate(mStartAngle,mCirclePoint.x,mCirclePoint.y);
        canvas.drawArc(mArcRect,0,mDefaultAngle,false,mArcPaint);
        canvas.drawArc(mArcRect,0,mSweepAngle,false,mArcProgressPaint);
        canvas.restore();  //修复
    }
    private void drawText(Canvas canvas){
        canvas.drawText("0",mCirclePoint.x,mValuePaintSet,mValuePaint);
    }
    private float measureTextHeight(Paint paint){
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();   //字体度量
        return Math.abs(fontMetrics.ascent -fontMetrics.descent);
        //Math.abs(fontMetrics.ascent )上两格的绝对值
        //descent  最后一格

    }
    private void startAnim(){
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 270);
        valueAnimator.setDuration(4000);
        valueAnimator.setInterpolator(new BounceInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float index=(float)animation.getAnimatedValue();
                mSweepAngle=index;
                invalidate();  //刷新页面
            }
        });
        valueAnimator.start();
    }
}
